<template>
  <div class="sidebar">
    <el-menu
      class="sidebar-el-menu"
      :default-active="onRoutes"
      :collapse="collapse"
      background-color="#324157"
      text-color="#bfcbd9"
      active-text-color="#20a0ff"
      unique-opened
      router
    >
      <template v-for="item in items">
          <el-submenu :index="item.path" :key="item.id">
            <template slot="title">
              <span slot="title">{{ item.authName }}</span>
            </template>
            <template v-for="subItem in item.children">
              <el-menu-item
                :index="subItem.path"
                :key="subItem.id"
              >
                <template slot="title">{{ subItem.authName }}</template>
              </el-menu-item>
            </template>
          </el-submenu>
      </template>
    </el-menu>
  </div>
</template>

<script>
import bus from "../components/bus";
import { getMenus } from "../api/index";
export default {
  data() {
    return {
      collapse: true,
      items:[],
    };
  },
  computed: {
    onRoutes() {
      return this.$route.path.replace("/", "");
    },
  },
  created() {
    getMenus().then((res) => {
      console.log(res);
      this.items=res.data
    });
    // 通过 Event Bus 进行组件间通信，来折叠侧边栏
    bus.$on("collapse", (msg) => {
      this.collapse = msg;
      bus.$emit("collapse-content", msg);
    });
  },
};
</script>

<style scoped>
.sidebar {
  display: block;
  position: absolute;
  left: 0;
  top: 70px;
  bottom: 0;
  overflow-y: scroll;
}
.sidebar::-webkit-scrollbar {
  width: 0;
}
.sidebar-el-menu:not(.el-menu--collapse) {
  width: 250px;
  background: #333744;
}
.sidebar > ul {
  height: 100%;
}
</style>
